﻿using GatewayClient.MQ;
using Models.Common;
using Newtonsoft.Json;
using RabbitMQ.Client;
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Threading;
using UtilsBase;

namespace GatewayClient.Service
{
    public static class ServiceMQ_SystemCommand
    {

        public static void systemProcessStart(MQReqModel<object> mQReqModel)
        {
            try
            {
                MQSystemCommand mQSystemCommand = UtilsObject.DynamicToEntity<MQSystemCommand>(mQReqModel.data);

                var process = new Process
                {
                    StartInfo = new ProcessStartInfo(mQSystemCommand.fileName, "")//"/bin/bash"
                };
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.UseShellExecute = false;
                process.Start();
                process.StandardInput.WriteLine(mQSystemCommand.command);
                process.StandardInput.Close();
                var cpuInfo = process.StandardOutput.ReadToEnd();

                var lines = cpuInfo.Split('\n');
                foreach (var item in lines)
                {
                    Console.WriteLine(DateTime.Now.ToString() + "行记录：" + item);
                }


                mQReqModel.respCode = "2";
                mQReqModel.respMsg = cpuInfo;
                new ServiceMQ_Helper().sendMsg(mQReqModel);

            }
            catch (Exception ex)
            {
                //更新状态       
                mQReqModel.respCode = "3";
                mQReqModel.respMsg = "system error";
                new ServiceMQ_Helper().sendMsg(mQReqModel);

                Console.WriteLine(ex.ToString());
            }

        }


    }
}
